package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.IUserDao;
import com.cn.wanxi.dao.impl.UserDaoImpl;
import com.cn.wanxi.model.PageModel;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.IUserService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserServiceImpl implements IUserService {
    IUserDao userDao = new UserDaoImpl();

    @Override
    public int addUser(UserModel userModel) {
        if (userIsExist(userModel.getUsername()) == false) {//如果为空，则该用户名为未被占用，false
            int count = userDao.addUser(userModel);
            return count;
        }else
            return 4;
        }

        @Override
        public int updateUser (UserModel userModel){
            return userDao.updateUser(userModel);
        }

        @Override
        public int deleteUser ( int id){
            return userDao.deleteUser(id);
        }

        @Override
        public UserModel getUser (int id){
            return userDao.getUser(id);
        }

    @Override
    public List<UserModel> getUserList() {
        return userDao.getUserList();
    }

    @Override
    public PageModel<UserModel> getUsers(Map<String ,Object> map) {
        PageModel pager = new PageModel();
        pager.setList(userDao.getUsers(map));
        pager.setTotal(userDao.getUserCount(map));
        return pager;
    }

    @Override
        public boolean userIsExist (String username){
            UserModel userModel = userDao.getUserByUsername(username);
            if (userModel == null) {//如果为空，则该用户名为未被占用，false
                return false;
            } else//如果不为空，则该用户名被已占用，true
                return true;
        }

    @Override
    public UserModel login(UserModel userModel) {
        return userDao.login(userModel);
    }

}
